import { HttpClient } from "@angular/common/http";
import { Component, OnInit } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { NavController } from "@ionic/angular";

@Component({
  selector: "app-phdetail",
  templateUrl: "./phdetail.page.html",
  styleUrls: ["./phdetail.page.scss"],
})
export class PHdetailPage implements OnInit {
  constructor(
    public router: NavController,
    public http: HttpClient,
    public route: ActivatedRoute
  ) {}
  res: Result;
  title: string;
  bid: number;
  cate: Cate;
  book_id = [];
  iseHistory: string;
  isExist: string;
  back() {
    location.replace("/tabs/tab1");
  }
  addHistory() {
    let url =
      "http://xiaoshuogebed.applinzi.com/book_history?title=" + this.title;
    this.http.get(url).subscribe((res) => {
      this.iseHistory = "true";
    });
  }
  addBook() {
    let url = "http://xiaoshuogebed.applinzi.com/addbook?title=" + this.title;
    this.http.get(url).subscribe((result) => {
      this.isExist = "true";
    });
  }
  ngOnInit() {
    let url =
      "http://xiaoshuogebed.applinzi.com/book_detail?title=" +
      this.route.snapshot.params.title;
    this.http.get(url).subscribe((res: Result) => {
      this.res = res;
      let title = res.results[res.results.length - 1].title;
      this.title = title;
      let bid = res.results[res.results.length - 1].bid;
      this.bid = bid;
    });
    let cateUrl =
      "http://xiaoshuogebed.applinzi.com/cate?title=" +
      this.route.snapshot.params.title;
    this.http.get(cateUrl).subscribe((result: Cate) => {
      this.cate = result;
    });
    let book_url = "http://xiaoshuogebed.applinzi.com/bookrack";

    this.http.get(book_url).subscribe((res: Result) => {
      for (let i = 0; i < res.results.length; i++) {
        this.book_id.push(res.results[i].bid);
      }
      if (this.book_id.indexOf(this.bid) > -1) {
        this.isExist = "true";
      } else {
        this.isExist = "false";
      }
    });
  }
}
interface Result {
  code: number;
  message: string;
  results: Details[];
}
interface Details {
  author: string;
  bid: number;
  cover: string;
  latelyFollower: string;
  majorCate: string;
  male_id: number;
  minorCate: string;
  shortIntro: string;
  title: string;
  click: string;
  numCount: string;
  recommended: string;
}
interface Cate {
  code: number;
  message: string;
  results: Cates[];
}
interface Cates {
  bid: number;
  cid: number;
  content: string;
  num_id: string;
  title: string;
  section: string;
}
